package com.hw.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hw.springcloud.entities.CommonResult;
import com.hw.springcloud.global.GlobalSentinelHandle;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname: RateLimitController
 * @Description: 描述
 * @Date: 2021/12/16
 * @Author: HuWei
 */
@RestController
@RequestMapping("/rateLimit")
public class RateLimitController {

    @GetMapping("/NameSentinel")
    @SentinelResource(value = "NameSentinel",blockHandler = "sentinelHandle")
    public CommonResult nameSentinel(){
        return new CommonResult(200,"根据资源名称设置Sentinel");
    }

    public CommonResult sentinelHandle(BlockException blockException){
        return new CommonResult(500,"被限流了~~~,"+blockException.getClass().getCanonicalName());
    }

    @GetMapping("/UrlSentinel")
    @SentinelResource(value = "UrlSentinel")
    public CommonResult urlSentinel(){
        return new CommonResult(200,"根据Url设置Sentinel");
    }

    /**
     * 自定义全局限流返回
     */
    @GetMapping("/globalSentinel")
    @SentinelResource(value = "globalSentinel",blockHandlerClass = GlobalSentinelHandle.class,blockHandler = "sentinelHandle2")
    public CommonResult globalSentinel(){
        return new CommonResult(200,"没有被全局限流,嘻嘻");
    }
}
